package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.DateUtil;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.CustomerBiz;
import com.egao.orderB.entity.Customer;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-12-21 15:17:37
 * @version ：1.0
 */
@Repository
public class CustomerBizImpl extends BaseDao implements CustomerBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Customer customer, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name");
    sql.append(",c.abbreviation,c.profile_picture,c.pinyin");
		sql.append(",c.code,c.account,c.social_code,c.business_license");
		sql.append(",c.agreement,c.agreement_start,c.agreement_end,c.settle");
		sql.append(",c.contacts,c.contacts_phone,c.salesmanId,c.salesman");
		sql.append(",c.operaterId,c.operater,c.address,c.invoice_state");
		sql.append(",c.register_date,c.type,c.state,c.message");
		sql.append(",c.gzh_send,c.offer_limit_num,c.order_num,c.create_time");
		sql.append(",c.create_user,c.update_time,c.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_customer c ");
    sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
    sql.append(" where c.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and c.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
      if(Utils.isNotEmpty(customer.getSort())&&Utils.isNotEmpty(customer.getOrder())){
        sql.append(" order by c."+customer.getSort()+" "+customer.getOrder());
      } else {
        sql.append(" order by c.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Customer> list(Customer customer, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
    StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name");
    sql.append(",c.abbreviation,c.profile_picture,c.pinyin");
		sql.append(",c.code,c.account,c.social_code,c.business_license");
		sql.append(",c.agreement,c.agreement_start,c.agreement_end,c.settle");
		sql.append(",c.contacts,c.contacts_phone,c.salesmanId,c.salesman");
		sql.append(",c.operaterId,c.operater,c.address,c.invoice_state");
		sql.append(",c.register_date,c.type,c.state,c.message");
		sql.append(",c.gzh_send,c.offer_limit_num,c.order_num,c.create_time");
		sql.append(",c.create_user,c.update_time,c.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_customer c ");
    sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
    sql.append(" where c.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and c.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(customer)){
			if(Utils.isNotEmpty(customer.getName())){
				sql.append(" and instr(c.name,:name)>0 ");
				map.put("name", customer.getName());
			}
      if(Utils.isNotEmpty(customer.getSort())&&Utils.isNotEmpty(customer.getOrder())){
        sql.append(" order by c."+customer.getSort()+" "+customer.getOrder());
      } else {
        sql.append(" order by c.create_time desc");
      }
    }
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Customer getById(Customer customer) {
		RowMapper<Customer> rm = new BeanPropertyRowMapper<Customer>(Customer.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select c.id,c.organization_id,c.name");
    sql.append(",c.abbreviation,c.profile_picture,c.pinyin");
		sql.append(",c.code,c.account,c.social_code,c.business_license");
		sql.append(",c.agreement,c.agreement_start,c.agreement_end,c.settle");
		sql.append(",c.contacts,c.contacts_phone,c.salesmanId,c.salesman");
		sql.append(",c.operaterId,c.operater,c.address,c.invoice_state");
		sql.append(",c.register_date,c.type,c.state,c.message");
		sql.append(",c.gzh_send,c.offer_limit_num,c.order_num,c.create_time");
		sql.append(",c.create_user,c.update_time,c.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from chf_customer c ");
    sql.append(" inner join sys_user suser on suser.user_id=c.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=c.organization_id and sogzn.deleted=0 ");
    sql.append(" where c.deleted=0 and c.id=:id ");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(Customer customer, User loginUser) {
		customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_customer (");
    sql.append("organization_id,name,abbreviation,profile_picture");
		sql.append(",pinyin,code,account");
		sql.append(",social_code,business_license,agreement,agreement_start");
		sql.append(",agreement_end,settle,contacts,contacts_phone");
		sql.append(",salesmanId,salesman,operaterId,operater");
		sql.append(",address,invoice_state,register_date,type");
		sql.append(",state,message,gzh_send,offer_limit_num");
		sql.append(",order_num,create_user,create_time) values ( ");
    sql.append(":organization_id,:name,:abbreviation,:profile_picture");
		sql.append(",:pinyin,:code,:account");
		sql.append(",:social_code,:business_license,:agreement,:agreement_start");
		sql.append(",:agreement_end,:settle,:contacts,:contacts_phone");
		sql.append(",:salesmanId,:salesman,:operaterId,:operater");
		sql.append(",:address,:invoice_state,:register_date,:type");
		sql.append(",:state,:message,:gzh_send,:offer_limit_num");
		sql.append(",:order_num,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("name", customer.getName());
    map.put("abbreviation", customer.getAbbreviation());
    map.put("profile_picture", customer.getProfilePicture());
    map.put("pinyin", customer.getPinyin());
    map.put("code", customer.getCode());
    map.put("account", customer.getAccount());
    map.put("social_code", customer.getSocialCode());
    map.put("business_license", customer.getBusinessLicense());
    map.put("agreement", customer.getAgreement());
    map.put("agreement_start", customer.getAgreementStart());
    map.put("agreement_end", customer.getAgreementEnd());
    map.put("settle", customer.getSettle());
    map.put("contacts", customer.getContacts());
    map.put("contacts_phone", customer.getContactsPhone());
    map.put("salesmanId", customer.getSalesmanid()==null?null:customer.getSalesmanid()+"");
    map.put("salesman", customer.getSalesman());
    map.put("operaterId", customer.getOperaterid()==null?null:customer.getOperaterid()+"");
    map.put("operater", customer.getOperater());
    map.put("address", customer.getAddress());
    map.put("invoice_state", customer.getInvoiceState()==null?null:customer.getInvoiceState()+"");
    map.put("register_date", customer.getRegisterDate());
    map.put("type", customer.getType());
    map.put("state", customer.getState());
    map.put("message", customer.getMessage()==null?null:customer.getMessage()+"");
    map.put("gzh_send", customer.getGzhSend()==null?null:customer.getGzhSend()+"");
    map.put("offer_limit_num", customer.getOfferLimitNum()==null?null:customer.getOfferLimitNum()+"");
    map.put("order_num", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Customer customer, User loginUser) {
		customer.setPinyin(PinyinUtils.ToFirstChar(customer.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_customer set ");
    sql.append("name=:name,abbreviation=:abbreviation,profile_picture=:profile_picture ");
		sql.append(",pinyin=:pinyin,code=:code ");
		sql.append(",social_code=:social_code,business_license=:business_license ");
		sql.append(",agreement=:agreement,agreement_start=:agreement_start,agreement_end=:agreement_end ");
		sql.append(",settle=:settle,contacts=:contacts,contacts_phone=:contacts_phone ");
		sql.append(",salesmanId=:salesmanId,salesman=:salesman,operaterId=:operaterId ");
		sql.append(",operater=:operater,address=:address,invoice_state=:invoice_state ");
		sql.append(",register_date=:register_date,type=:type,state=:state ");
		sql.append(",message=:message,gzh_send=:gzh_send,offer_limit_num=:offer_limit_num ");
		sql.append(",order_num=:order_num,update_time=now(),update_user=:update_user where id=:id ");
    map.put("name", customer.getName());
    map.put("abbreviation", customer.getAbbreviation());
    map.put("profile_picture", customer.getProfilePicture());
    map.put("pinyin", customer.getPinyin());
    map.put("code", customer.getCode());
    map.put("social_code", customer.getSocialCode());
    map.put("business_license", customer.getBusinessLicense());
    map.put("agreement", customer.getAgreement());
    map.put("agreement_start", customer.getAgreementStart());
    map.put("agreement_end", customer.getAgreementEnd());
    map.put("settle", customer.getSettle());
    map.put("contacts", customer.getContacts());
    map.put("contacts_phone", customer.getContactsPhone());
    map.put("salesmanId", customer.getSalesmanid()==null?null:customer.getSalesmanid()+"");
    map.put("salesman", customer.getSalesman());
    map.put("operaterId", customer.getOperaterid()==null?null:customer.getOperaterid()+"");
    map.put("operater", customer.getOperater());
    map.put("address", customer.getAddress());
    map.put("invoice_state", customer.getInvoiceState()==null?null:customer.getInvoiceState()+"");
    map.put("register_date", customer.getRegisterDate());
    map.put("type", customer.getType());
    map.put("state", customer.getState());
    map.put("message", customer.getMessage()==null?null:customer.getMessage()+"");
    map.put("gzh_send", customer.getGzhSend()==null?null:customer.getGzhSend()+"");
    map.put("offer_limit_num", customer.getOfferLimitNum()==null?null:customer.getOfferLimitNum()+"");
    map.put("order_num", customer.getOrderNum()==null?null:customer.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", customer.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Customer customer, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(customer.getId().length()>0){
			for(String ID : customer.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_customer set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
